﻿<%@ WebHandler Language="C#" Class="ItemsManage" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class ItemsManage : AjaxHandlerBase
{
    public ItemsManage()
    {
        //新增
        htTypePermit.Add("add", Constants.Permit_Linkage_AddItem);
        //编辑
        htTypePermit.Add("edit", Constants.Permit_Linkage_EditItem);
        //删除
        htTypePermit.Add("del", Constants.Permit_Linkage_DelItem);
    }
    
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";

            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //添加报备相关信息
                case "getlist":
                    GetList(context);
                    break;
                case "add":
                    AddItem(context);
                    break;
                case "edit":
                    EditItem(context);
                    break;
                case "del":
                    DelItem(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }

    //查询
    private void GetList(HttpContext context)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(" 1=1 and isdel=0 ");
        if (!String.IsNullOrEmpty(context.Request["keyword"]))
        {
            sb.AppendFormat(" and ItemsName like '%{0}%'",context.Request["keyword"]);
        }
        if (!String.IsNullOrEmpty(context.Request["CompanyId"]))
        {
            sb.AppendFormat(" and CompanyId='{0}' ",context.Request["CompanyId"]);
        }
        else if (!String.IsNullOrEmpty(context.Request["CityId"]))
        {
            sb.AppendFormat(" and CityId='{0}' ", context.Request["CityId"]);
        }
        else
        {
            sb.AppendFormat(" and CompanyId='{0}' ", CurrentUserInfo.CompanyId);
        }
        //页码
        int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
        //页显示行数
        int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数
        string strOrderField = "ItemsName";
        DataSet ds = Chenxin.Lib.Business.DAL.System_TraceInfo.GetList(strPageIndex, strPageSize, strOrderField, "t_Linkage_ItemInfo", sb.ToString(), "*", "*", true);

        context.Response.Write(string.Format("{{success:1,msg:'',data:{{count:{0},{1}}}}}", ds.Tables[1].Rows[0][0], Chenxin.Lib.Utility.JsonHelp.Convert2Json(ds.Tables[0]))); 
    }
    
    //新增
    private void AddItem(HttpContext context)
    {
        Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo info = (Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo));
        string strwhere = string.Format("ItemsName='{0}' and CompanyId='{1}' and IsDel=0 ", info.ItemsName, info.CompanyId);
        List<Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo> list = Chenxin.Lib.Business.DAL.t_Linkage_ItemInfo.GetListArray(strwhere);
        if (list.Count > 0)
        {
            context.Response.Write(string.Format("{{success:0,msg:'该项目名称已存在！新增失败！',data:''}}"));
            return;
        }
        
        info.RecordId = Guid.NewGuid();
        info.OperaterId = CurrentUserInfo.UserId;
        info.OperaterName = CurrentUserInfo.UserName;
        info.AddTime = DateTime.Now;
        info.IsDel = false;
        Chenxin.Lib.Business.DAL.t_Linkage_ItemInfo.Add(info);

        string text = CurrentUserInfo.UserName + "：新增联动项目。项目名称：" + info.ItemsName;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_AddItem"].ToString());

        context.Response.Write(string.Format("{{success:1,msg:'新增项目成功！',data:''}}"));
    } 
    
    //编辑
    private void EditItem(HttpContext context)
    {
        Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo info = (Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo));
        string strwhere = string.Format("ItemsName='{0}' and RecordId<>'{1}' and CompanyId='{2}' and IsDel=0 ", info.ItemsName, info.RecordId,info.CompanyId);
        List<Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo> list = Chenxin.Lib.Business.DAL.t_Linkage_ItemInfo.GetListArray(strwhere);
        if (list.Count > 0)
        {
            context.Response.Write(string.Format("{{success:0,msg:'该项目名称已存在！编辑失败！',data:''}}"));
            return;
        }
        Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo temp = Chenxin.Lib.Business.DAL.t_Linkage_ItemInfo.GetModel(info.RecordId);
        temp.ItemsAddress = info.ItemsAddress;
        temp.ItemsName = info.ItemsName;
        temp.ItemsRegion = info.ItemsRegion;
        temp.CompanyId = info.CompanyId;
        temp.CompanyName = info.CompanyName;
        temp.CityId = info.CityId;
        temp.CityName = info.CityName;
        
        Chenxin.Lib.Business.DAL.t_Linkage_ItemInfo.Update(temp);
        
        string text = CurrentUserInfo.UserName + "：编辑联动项目。项目名称：" + info.ItemsName;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_EditItem"].ToString());

        context.Response.Write(string.Format("{{success:1,msg:'编辑项目成功！',data:''}}"));
    }
    
    //删除
    private void DelItem(HttpContext context)
    {
        string modelid = context.Request["modelid"];

        Chenxin.Lib.Business.Entity.t_Linkage_ItemInfo info = Chenxin.Lib.Business.DAL.t_Linkage_ItemInfo.GetModel(new Guid(modelid));
        info.IsDel = true;
            
        Chenxin.Lib.Business.DAL.t_Linkage_ItemInfo.Update(info);

        string text = CurrentUserInfo.UserName + "：删除联动项目。项目名称：" + info.ItemsName;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_DelItem"].ToString());
        
        context.Response.Write(string.Format("{{success:1,msg:'删除项目成功！',data:''}}"));
    }
   
}